<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="公司名称" prop="compName">
        <el-input
          v-model="queryParams.compName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职业" prop="profession">
        <el-input
          v-model="queryParams.profession"
          placeholder="请输入职业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="行业" prop="industry">
        <el-input
          v-model="queryParams.industry"
          placeholder="请输入行业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cardList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="头像" align="center" prop="img">
        <template slot-scope="scope">
          <el-image :src=scope.row.img height="100" width="130"/>
        </template>
      </el-table-column>

      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column label="电话" align="center" prop="tel"/>
      <el-table-column label="老家地址省" align="center" prop="addressProv"/>
      <el-table-column label="老家地址市" align="center" prop="addressCity"/>
      <el-table-column label="现居地" align="center" prop="addressPresent"/>
      <el-table-column label="公司名称" align="center" prop="compName"/>
      <el-table-column label="职业" align="center" prop="profession"/>
      <el-table-column label="行业" align="center" prop="industry"/>
      <el-table-column label="邮箱" align="center" prop="email"/>
      <!--      <el-table-column label="微信二维码" align="center" prop="wxCode"/>-->
      <el-table-column label="个人介绍" align="center" prop="perDescribe">
        <template slot-scope="scope">
          <div class="ellipsis">{{ scope.row.perDescribe }}</div>
        </template>
      </el-table-column>
      <!--      <el-table-column label="公司介绍-图片" align="center" prop="compDescribeImg"/>-->
      <!--      <el-table-column label="公司介绍-视频" align="center" prop="compDescribeVideo"/>-->
      <!--      <el-table-column label="帐号状态" align="center" prop="status"/>-->
      <el-table-column label="实名认证" align="center" prop="isReal" :formatter="formatReal"/>
      <el-table-column label="是否生效" align="center" prop="isCard" :formatter="formatCard"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            style="margin-left: 10px"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['lxzx:card:edit']"
          >查看名片
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row)"
            v-hasPermi="['lxzx:card:edit']"
          >认证审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['lxzx:card:remove']"
          >名片失效
          </el-button>
<!--          <el-popconfirm title="确定删除?" @confirm="handleDel(scope.row)">-->
<!--            <template #reference>-->
<!--              <el-button-->
<!--                size="mini"-->
<!--                type="text"-->
<!--                icon="el-icon-edit"-->
<!--                v-hasPermi="['lxzx:card:del']"-->
<!--              >删除-->
<!--              </el-button>-->
<!--            </template>-->
<!--          </el-popconfirm>-->

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户名片对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户基础信息id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户基础信息id"/>
        </el-form-item>
        <el-form-item label="头像" prop="img">
          <el-image :src="form.img" height="100" width="130"/>
        </el-form-item>
        <el-form-item label="电话" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入电话"/>
        </el-form-item>
        <el-form-item label="老家地址省" prop="addressProv">
          <el-input v-model="form.addressProv" placeholder="请输入老家地址"/>
        </el-form-item>
        <el-form-item label="老家地址市" prop="addressCity">
          <el-input v-model="form.addressCity" placeholder="请输入老家地址"/>
        </el-form-item>
        <el-form-item label="现居地" prop="addressPresent">
          <el-input v-model="form.addressPresent" placeholder="请输入老家地址"/>
        </el-form-item>
        <el-form-item label="老家地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入老家地址"/>
        </el-form-item>
        <el-form-item label="公司名称" prop="compName">
          <el-input v-model="form.compName" placeholder="请输入公司名称"/>
        </el-form-item>
        <el-form-item label="职业" prop="profession">
          <el-input v-model="form.profession" placeholder="请输入职业"/>
        </el-form-item>
        <el-form-item label="行业" prop="industry">
          <el-input v-model="form.industry" placeholder="请输入行业"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱"/>
        </el-form-item>
        <el-form-item label="微信二维码" prop="wxCode">
          <el-image :src="form.wxCode" height="100" width="130"/></el-form-item>
        <el-form-item label="个人介绍" prop="perDescribe">
          <el-input v-model="form.perDescribe" type="textarea" placeholder="请输入个人介绍"/>
        </el-form-item>
        <el-form-item label="公司介绍-图片" prop="compDescribeImg">
          <!--          <el-input v-model="form.compDescribeImg" placeholder="请输入公司介绍-图片"/>-->
          <el-image :src="form.compDescribeImg" height="100" width="130"/>
        </el-form-item>
        <el-form-item label="公司介绍-视频" prop="compDescribeVideo">
          <!--          <el-input v-model="form.compDescribeVideo" placeholder="请输入公司介绍-视频"/>-->
          <video width="320" height="240" controls>
            <source :src="form.compDescribeVideo" type="video/mp4">
            您的浏览器不支持 video 标签。
          </video>
        </el-form-item>
        <!--        <el-form-item label="删除标志" prop="delFlag">-->
        <!--          <el-input v-model="form.delFlag" placeholder="请输入删除标志"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <!--      <div slot="footer" class="dialog-footer">-->
      <!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
      <!--        <el-button @click="cancel">取 消</el-button>-->
      <!--      </div>-->
    </el-dialog>

    <el-dialog :title="title" :visible.sync="audit" width="600px" append-to-body>
      <el-form ref="auditForm" :model="auditForm" :rules="rules" label-width="80px">
        <el-form-item label="身份证正面" prop="justImg">
          <el-image :src="auditForm.justImg" height="100" width="130"/>
        </el-form-item>
        <el-form-item label="身份证反面" prop="againstImg">
          <el-image :src="auditForm.againstImg" height="100" width="130"/>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="auditForm.name" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="identityNo">
          <el-input v-model="auditForm.identityNo" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="审核状态" prop="name">
          <!--          <el-input v-model="auditForm.status" placeholder="请选择审核状态" />-->
          <el-select v-model="auditForm.status">
            <el-option
              v-for="option in statusOption"
              :key="option.value"
              :label="option.label"
              :value="option.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="auditForm.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="auditSubmitForm">确 定</el-button>
        <el-button @click="auditCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCard, getCard, delCard, addCard, updateCard} from '@/api/lxzx/card'
import { delReal, getRealByUserId, updateReal } from '@/api/lxzx/real'
import { resetUserPwd } from '@/api/system/user'

export default {
  name: 'Card',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户名片表格数据
      cardList: [],
      // 弹出层标题
      title: '',
      auditTitle: '',
      // 是否显示弹出层
      open: false,
      audit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        img: null,
        tel: null,
        address: null,
        compName: null,
        profession: null,
        industry: null,
        email: null,
        wxCode: null,
        describe: null,
        compDescribeImg: null,
        compDescribeVideo: null,
        status: null
      },
      // 表单参数
      form: {},
      auditForm: {},
      // 表单校验
      rules: {},
      statusOption: [
        {
          value: '1',
          label: '待审核'
        },
        {
          value: '2',
          label: '审核通过'
        },
        {
          value: '3',
          label: '审核驳回'
        }
      ],
      statusDict:{
        "1":"待审核",
        "2":"审核通过",
        "3":"审核驳回",
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    formatReal(row) {
      return this.statusDict[row.isReal] || row.isReal;
    },
    formatCard(row) {
      return row.isCard==='1'?'生效':'已失效';
    },
    /** 查询用户名片列表 */
    getList() {
      this.loading = true
      listCard(this.queryParams).then(response => {
        this.cardList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 取消按钮
    auditCancel() {
      this.audit = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        img: null,
        tel: null,
        address: null,
        compName: null,
        profession: null,
        industry: null,
        email: null,
        wxCode: null,
        describe: null,
        compDescribeImg: null,
        compDescribeVideo: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加用户名片'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCard(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '查看用户名片'
      })
    },
    handleAudit(row) {
      this.reset()
      const id = row.userId
      getRealByUserId(id).then(response => {
        if(response.data == null){
          alert("未进行实名认证，无法审核")
        }else {
          this.auditForm = response.data
          this.audit = true
          this.auditTitle = '实名审核'
        }
      })
    },
    handleDel(row){
      delReal(row.id).then(response => {

      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCard(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addCard(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    auditSubmitForm() {
      this.$refs['auditForm'].validate(valid => {
        if (valid) {
          if (this.auditForm.id != null) {
            updateReal(this.auditForm).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.audit = false
              this.getList()
            })
          } else {
            addCard(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$prompt('是否确认设置用户名为"' + row.name + '"的名片失效？若确定，请填写原因', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputPattern: /^.{1,200}$/,
        inputErrorMessage: "请填写删除原因",
        inputPlaceholder:'请填写删除原因'
      }).then(({ value }) => {
        delCard(ids,value).then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        }).catch(() => {
        })
      }).catch(() => {});
      // this.$modal.prompt('是否确认删除用户名为"' + row.name + '"的数据项？若确定，请填写备注').then(function (val) {
      //   console.log(val)
      //   if(!val.value){
      //     return
      //   }
      //   return delCard(ids)
      // }).then(() => {
      //   this.getList()
      //   this.$modal.msgSuccess('删除成功')
      // }).catch(() => {
      // })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('lxzx/card/export', {
        ...this.queryParams
      }, `card_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style>
.ellipsis {
  white-space: nowrap; /* 不换行 */
  overflow: hidden; /* 超出部分隐藏 */
  text-overflow: ellipsis; /* 使用省略号表示超出部分 */
}
</style>

